package com.xzj.admin.resource.sync.controller;

import com.xzj.admin.resource.sync.domain.vo.SelectOption;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import com.xzj.common.id.gen.DefaultIdHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.resource.sync.domain.entity.ResMessageTarget;
import com.xzj.admin.resource.sync.service.ResMessageTargetService;

import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: ResMessageTargetController
 * @description: 资源消息目标端;(res_message_target)表控制层
 * @author: XiaZunJun
 * @create: 2024-10-31
 */
@Tag(name = "资源消息目标端对象功能接口")
@RestController
@RequestMapping("/resMessageTarget")
public class ResMessageTargetController{
    @Resource
    private ResMessageTargetService resMessageTargetService;

    /**
     * 通过ID查询单条数据
     *
     * @param targetId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{targetId}")
    public ResMessageTarget queryById(@PathVariable(name = "targetId") String targetId){
        return resMessageTargetService.queryById(targetId);
    }

    @Operation(summary ="查询所有目标端数据")
    @GetMapping("/queryAll")
    public List<SelectOption> queryAll(){
        return resMessageTargetService.queryAll();
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<ResMessageTarget> pageQuery(@RequestBody PageReq<ResMessageTarget> pageReq){
        return resMessageTargetService.pageQuery(pageReq);
    }

    /**
     * 新增数据
     *
     * @param resMessageTarget 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public ResMessageTarget addOrUpdate(@RequestBody ResMessageTarget resMessageTarget){
        if(ObjectUtil.isNotEmpty(resMessageTarget.getTargetId())){
            resMessageTargetService.update(resMessageTarget);
        }else{
            resMessageTarget.setTargetId(DefaultIdHelper.nextIdStr());
            resMessageTargetService.insert(resMessageTarget);
        }
        return resMessageTarget;
    }

    /**
     * 通过主键删除数据
     *
     * @param targetId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{targetId}")
    public boolean deleteById(@PathVariable(name = "targetId") String targetId){
        return resMessageTargetService.deleteById(targetId);
    }

    @Operation(summary = "查询所有目标端资源")
    @GetMapping("/queryAll")
    public List<SelectOption> queryAll(){
        return resMessageTargetService.queryAll();
    }

}